<?php
/**
 * 会员管理-xhq
 */
namespace App\Http\Controllers\Admin\Member;

use App\Http\Controllers\AdminController;
use App\Http\Requests\StoreMember;
use Illuminate\Support\Facades\DB;
use Illuminate\Http\Request;

class MemberController extends AdminController
{
    public function __construct()
    {
        parent::__construct();
        $this->view = 'Admin.Member.';
    }

    /**
     * 列表页
     */
    public function index(Request $request)
    {
        $data = array();
        $user_role = $request->input('user_role');
        $phone = $request->input('phone');
        $status = $request->input('status');
        $search = array(
            'user_role'=>$user_role,
            'phone'=>$phone,
        );
        if(!empty($status)){
            $search['status'] = $status;
        }
        $data['list'] = DB::table('user')->where('status','=',1);

        foreach ($search as $k => $v){
            if(!empty($v)){
                $data['list'] = $data['list']->where($k,$v);
            }
        }
        $data['list'] = $data['list']->paginate(10);
        $data['search'] = $search;

        return \Helpers::showViewCURD($this->view.'index',3,'',$data);
    }

    public function edit($id)
    {
        $data['id'] = $id;
        //获取省的信息
        $data['privoide'] = DB::table('area')
            ->select('id','name')
            ->where(['pid'=>0])
            ->get();
        $data['editList'] = DB::table('user')
                                ->select('province_id','city_id','area_id','point','user_role')
                                ->where('id',$data['id'])
                                ->where('status',1)
                                ->first();
        if(!empty($data['editList']->province_id)){
            //获取市的信息
            $data['city'] = DB::table('area')
                ->select('id','name')
                ->where(['pid'=>$data['editList']->province_id])
                ->get();
        }
        if(!empty($data['editList']->city_id))
        {
            //获取区的信息
            $data['area'] = DB::table('area')
                ->select('id','name')
                ->where(['pid'=>$data['editList']->city_id])
                ->get();
        }
        return \Helpers::showViewCURD($this->view.'set',2,'设置',$data);
    }

    public function update($id)
    {
        if(empty($id)) abort(404);
        unset($_POST['_method']);
        unset($_POST['_token']);
        $res = DB::table('user')
                ->where('id',$id)
                ->update($_POST);
        if($res)
        {
            $errors = '修改成功';
        }else{
            $errors = '修改失败';
        }
        //根据入库结果返回状态
        $errors = $res ? '修改成功!' : '修改失败!';
        $isSuccess = $res ? 'success' : 'error';
        \Helpers::insertUserActionLog('member','up',$id,$isSuccess,$_POST);
        return redirect()->back()->withErrors($errors);
    }

    /**
     * 删除操作...
     */
    public function del($id)
    {
        //验证传参,ac 1.逻辑删除 2.物理删除 3.还原
        if(empty($id) || empty($_POST['ac'])) abort(404);
        $status = $_POST['ac'];
        //status:1.启用2.禁用3.删除
        if($status == 1){
            $res = DB::table('user')->where('id',intval($id))->update(array('status'=>1));
            $preMsg = '还原';
            $op = 'open';
        }
        if($status == 2){
            $res = DB::table('user')->where('id',intval($id))->update(array('status'=>2));
            $preMsg = '删除';
            $op = 'stop';
        }
        if($status == 3) {
            $res = DB::table('user')->where('id',intval($id))->delete();
            $preMsg = '删除';
            $op = 'del';
        }
        $errorCode = $res ? 1 : -1;
        $msg = $res ? $preMsg.'成功!' : $preMsg.'失败!';
        $isSuccess = $res ? 'success' : 'error';
        \Helpers::insertUserActionLog('member',$op,$id,$isSuccess,$_POST);
        return json_encode(array('error'=>$errorCode,'msg'=>$msg));
    }

    /**
     * 数据还原...
     */
    public function back($id)
    {
        //验证传参,ac 1.逻辑删除 2.物理删除 3.还原
        if(empty($id) || empty($_POST['ac'])) abort(404);
        //还原逻辑删除数据
        $res = DB::table('user')->where('id',intval($id))->update(array('status'=>1));
        $preMsg = '还原';
        $errorCode = $res ? 1 : -1;
        $msg = $res ? $preMsg.'成功!' : $preMsg.'失败!';
        $isSuccess = $res ? 'success' : 'error';
        \Helpers::insertUserActionLog('member','back',$id,$isSuccess,$_POST);
        return json_encode(array('error'=>$errorCode,'msg'=>$msg));
    }

    /**
     * 回收站...
     */
    public function rb(Request $request)
    {
        $data = array();
        $user_role = $request->input('user_role');
        $phone = $request->input('phone');
        $status = $request->input('status');
        $search = array(
            'user_role'=>$user_role,
            'phone'=>$phone,
        );
        if(!empty($status)){
            $search['status'] = $status;
        }
        $data['list'] = DB::table('user')->where('status',2);

        foreach ($search as $k => $v){
            if(!empty($v)){
                $data['list'] = $data['list']->where($k,$v);
            }
        }
        $data['list'] = $data['list']->paginate(10);
        $data['search'] = $search;

        return \Helpers::showViewCURD($this->view.'index',4,'',$data);
    }

    public function getcity()
    {
        if(!empty($_POST['id']))
        {
            $res = DB::table("area")
                ->select('id','name')
                ->where(['pid'=>$_POST['id']])
                ->get();
            if(empty($res))
            {
                echo json_encode(['code'=>-1,'msg'=>'未获取到数据']);
            }else{
                $str = '<option value="">-- 请选择 --</option>';
                foreach($res as $v)
                {
                    $str .= '<option value="'.$v->id.'">'.$v->name.'</option>';
                }
                echo json_encode(['code'=>1,'msg'=>$str]);
            }

        }else{
            echo json_encode(['code'=>-1,'msg'=>'参数有误']);
        }
    }



}